<template>
  <div class="app-container">
    <!--订单详情-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">订单编号:</label>
        {{ soddOrderId }}
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :append-to-body="true" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="订单编号" prop="oddOrderId">
            {{ form.oddOrderId=soddOrderId }}
          </el-form-item>
          <el-form-item label="产品编号" prop="oddProdId">
            <el-input v-model="form.oddProdId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="数量" prop="oddCount">
            <el-input v-model="form.oddCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位">
            <el-select v-model="form.oddUnit" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.odd_unit"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品单价">
            <el-input v-model="form.oddPrice" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :close-on-click-modal="false" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="oddId" label="订单明细编号" />
        <el-table-column prop="oddOrderId" label="订单编号" />
        <el-table-column prop="oddProdId" label="商品名称" width="130px" align="center">
          <template slot-scope="scope">
            <product :cid="cid=scope.row.oddProdId" class="hhh" />
          </template>
        </el-table-column>
        <el-table-column prop="oddCount" label="数量" />
        <el-table-column prop="oddUnit" label="单位">
          <template slot-scope="scope">
            {{ dict.label.odd_unit[scope.row.oddUnit] }}
          </template>
        </el-table-column>
        <el-table-column prop="oddPrice" label="产品单价" />
        <el-table-column v-if="checkPer(['admin','ordersLine:edit','ordersLine:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

    </div>
  </div>
</template>

<script>
import product from './product.vue'
import crudOrdersLine from '@/api/ordersLine'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { oddId: null, oddOrderId: null, oddProdId: null, oddCount: null, oddUnit: null, oddPrice: null }
export default {
  name: 'OrdersLine',
  components: { pagination, crudOperation, udOperation, product },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  props: {
    soddOrderId: {
      type: Number,
      default: 0
    }
  },
  dicts: ['odd_unit'],
  cruds() {
    return CRUD({ queryOnPresenterCreated: false, title: '订单详情', url: 'api/ordersLine', idField: 'oddId', sort: 'oddId,desc', crudMethod: { ...crudOrdersLine }})
  },
  data() {
    return {
      cid: '',
      permission: {
        add: ['admin', 'ordersLine:add'],
        edit: ['admin', 'ordersLine:edit'],
        del: ['admin', 'ordersLine:del']
      },
      rules: {
        oddOrderId: [
          { required: true, message: '请先选中订单', trigger: 'blur' }
        ],
        oddProdId: [
          { required: true, message: '产品编号不能为空', trigger: 'blur' }
        ],
        oddCount: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'oddOrderId', display_name: '订单编号' },
        { key: 'oddProdId', display_name: '产品编号' }
      ]
    }
  },
  watch: {
    'soddOrderId'() {
      this.crud.params = { oddOrderId: this.soddOrderId }
      this.crud.toQuery()
    }
  },
  created() {
    this.crud.params = { oddOrderId: 0 }
    this.crud.toQuery()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
.hhh{
   margin-top: -3.8rem;
  height:6rem;
}
</style>
